package view;

import processing.core.PApplet;

//////////////////////////////////////////////////////////////////////////////
//Creates a list that includes any number of display objects in the scene
public class DisplayList {
	private V_DisplayFeature[] UIList;
	private boolean visible;
	private boolean sorted = false;
	private PApplet parent; 

	public DisplayList(boolean v, PApplet p) {
		visible = v;
		UIList = new V_DisplayFeature[0];
		parent = p;
	}

	public void addUIObject(V_DisplayFeature UIObject) // adds view objects to this list
	{
		V_DisplayFeature[] tmpList = UIList; // create a copy of the list
		UIList = new V_DisplayFeature[UIList.length+1]; // make room for new element
		for(int i = 0; i < tmpList.length; i++) {
			UIList[i] = tmpList[i]; // add the elements back in
		}
		UIList[tmpList.length] = UIObject; // add the new element to the end
		sorted = false; // set the list to be re-sorted on the next display command
	}

	public void displayUI() {
		if (sorted == false) {
			sorted = true;
			UIList = sortElements(UIList);
		}

		if (visible) {
			for (int i = 0; i < UIList.length; i++) {
				UIList[i].display(parent);
			}
		}
	}

	// Removes an object from the display list:
	public void removeDisplayFeature(V_DisplayFeature feature) {
		V_DisplayFeature[] tmpList = UIList;
		UIList = new V_DisplayFeature[UIList.length - 1];
		int s = 0;
		for (int i = 0; i < tmpList.length; i++) {
			if (tmpList[i].equals(feature) == false)
				UIList[i - s] = tmpList[i];
			else
				s = 1;
		}
	}

	// SORT the display elements based on display priority
	// currently implement an insertion sort
	private V_DisplayFeature[] sortElements(V_DisplayFeature[] objList) {
		int i, j; // index and sub-index values
		V_DisplayFeature temp;
		V_DisplayFeature[] tmpList = objList;

		for (i = 1; i < tmpList.length; i++) {
			j = i;
			temp = tmpList[j]; // store this item temporarily
			while (j > 0
					&& tmpList[j - 1].getDisplayPriority() > temp
							.getDisplayPriority()) {
				tmpList[j] = tmpList[j - 1];
				j--; // count backwards until you find the proper location to
						// place this element
			}
			tmpList[j] = temp;
		}

		return tmpList;
	}

	// Moves the given UI object (V_DisplayFeature) to the end of the list so
	// that it is drawn first
	// Does this by adjusting priority and then sorting the elements.
	public void bringToFront(V_DisplayFeature UI_obj) {
		// println("setting priority higher than: " +
		// UI_obj.getDisplayPriority());
		for (int i = 0; i < UIList.length; i++) {
			UIList[i].setDisplayPriority(0);
		}
		UI_obj.setDisplayPriority(10);
		UIList = sortElements(UIList);
	}

	// Utility to visually check the display priority of a given list of display
	// objects.
	public void printDisplayPriority(V_DisplayFeature[] objList) {
		for (int i = 0; i < objList.length; i++) {
			System.out.print(objList[i].getDisplayPriority() + ", ");
		}
		System.out.println();
	}

	// --------------------------------
	// Gets & Sets
	// --------------------------------
	public void setVisible(boolean val) {
		visible = val;
	}

	public boolean getVisible() {
		return visible;
	}
}
